<template>
  <div>
    <el-button class="inst" size="mini" @click="isShowSet = true">添加学生</el-button>
    <el-table :data="studentTable" style="width: 100%;">
      <el-table-column label="id" width="150">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.id }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="姓名" width="150">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.name }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="年龄" width="150">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.age }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="性别" width="150">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.sex }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="班级" width="150">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.class }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="upStudentTable(scope.$index,isShowUp = true)">修改</el-button>
          <el-button size="mini" type="danger" @click="delStudent(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-dialog title="添加学生信息" :visible.sync="isShowSet" width="30%">
      <el-form>
        <el-form-item label="编号">
          <el-input v-model="student.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="student.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="student.age"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="student.sex"></el-input>
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="student.class"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setStudent">确 定</el-button>
        <el-button @click="isShowSet = false">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog :data="upStudent" title="修改学生信息" :visible.sync="isShowUp" width="30%">
      <el-form>
        <!-- <template slot-scope="scope"> -->
        <el-form-item label="编号">
          <el-input :disabled="true" v-model="upStudent.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="upStudent.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="upStudent.age"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="upStudent.sex"></el-input>
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="upStudent.class"></el-input>
        </el-form-item>
        <!-- </template> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updStudent(upStudent)">确 定</el-button>
        <el-button @click="isShowUp = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
export default {
  // props:["student"],
  data() {
    return {
      // 学生表总信息
      studentTable: [],
      // 点击添加时出现弹窗
      isShowSet: false,
      // 点击修改时出现弹窗
      isShowUp: false,
      // 添加的学生信息
      student: {},
      // 修改时获取的当前索引的学生信息
      upStudent: {},
    };
  },
  methods: {
    // ------------------------------学生页面显示内容-----------------------------
    // 查询学生列表，显示在页面上
    getStudent() {
      axios
        .get("http://127.0.0.1:3000/student")
        .then((res) => {
          this.studentTable = res.data;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 插入学生信息
    setStudent() {
      axios
        .post(`${process.env.VUE_APP_BASE_URL}/student`, { student: this.student })
        .then(() => {
          this.getStudent();
          this.isShowSet = false;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 删除学生信息
    delStudent(index) {
      console.log(1111);
      axios.delete(`${process.env.VUE_APP_BASE_URL}/student${index}`).then(() => {
        console.log(index);
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        this.getStudent();
      });
    },
    // 修改学生信息
    updStudent() {
      axios
        .put(`${process.env.VUE_APP_BASE_URL}/student`, { student: this.upStudent })
        .then((res) => {
          this.$message({
            type: "success",
            message: "修改成功!",
          });
          this.getStudent();
          this.isShowUp = false;
          console.log(res);
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 用于获取需要修改的学生信息，显示在弹窗内
    upStudentTable(index) {
      for (let i = 0; i <= this.studentTable.length; i++) {
        if (i == index) {
          this.upStudent = this.clone(this.studentTable[i]);
        }
      }
    },
    // 克隆数据
    clone(obj) {
      // {name:"123"}
      let newObj = {};
      for (var key in obj) {
        newObj[key] = obj[key];
      }
      return newObj;
    },
  },
  created() {
    this.getStudent();
  },
};
</script>
<style scoped>
.inst {
  background-color: rgb(94, 189, 245);
  float: right;
  margin: 20px 50px 0px 0px;
  color: white;
}
</style>